#include <amxmodx>
#include <amxmisc>

new configfolder[200]

new execute[10][400]
new results[11]

new bool:voting

new admin

new show_pcvar, execute_pcvar

public plugin_init()
{
	register_plugin("Custom Vote Menus","1.0","GHW_Chronic")

	register_concmd("amx_cvote","command_vote",ADMIN_MENU," <vote name> ")
	register_concmd("amx_cvote_list","command_list",ADMIN_MENU," Lists all custom votes in the votes directory. ")

	register_menu("VoteMenu",1023,"VotePushed")

	show_pcvar = register_cvar("cvote_show","1")
	execute_pcvar = register_cvar("cvote_execute","1")

	get_configsdir(configfolder,199)
	format(configfolder,199,"%s/votes",configfolder)
}

public command_list(id,level,cid)
{
	if(!cmd_access(id,level,cid,1))
	{
		return PLUGIN_HANDLED
	}

	if(dir_exists(configfolder))
	{
		new file[32]
		new handle = open_dir(configfolder,file,31)
		if(strlen(file))
		{
			console_print(id,"[AMXX] The following are available vote menus: ")
			console_print(id,"[AMXX] ---")
			while(next_file(handle,file,31))
			{
				if(contain(file,".ini")==strlen(file)-4)
				{
					replace(file,31,".ini","")
					console_print(id,"[AMXX] %s",file)
				}
			}
			console_print(id,"[AMXX] ---")
		}
		else
		{
			console_print(id,"[AMXX] There are no vote menus created in the configs/votes directory.")
		}
		close_dir(handle)
	}
	else
	{
		console_print(id,"[AMXX] configs/votes directory does not exist.")
	}

	return PLUGIN_HANDLED
}

public command_vote(id,level,cid)
{
	if(!cmd_access(id,level,cid,2))
	{
		return PLUGIN_HANDLED
	}

	new arg[32]
	read_argv(1,arg,31)

	new configfile[200]
	format(configfile,199,"%s/%s.ini",configfolder,arg)

	if(voting)
	{
		console_print(id,"[AMXX] Another vote is currently in progress.")
	}
	else if(!file_exists(configfile))
	{
		console_print(id,"[AMXX] Menu ^"%s^" not found in menus folder.",arg)
	}
	else
	{
		new motd[2000], read[400], trash, part, keys

		for(new i=0;i<10;i++) execute[i] = ""

		new fsize = file_size(configfile,1)
		for(new i=0;i<fsize;i++)
		{
			read_file(configfile,i,read,399,trash)
			if(!part)
			{
				if(containi(read,"[Menu]")==0)
				{
					part = -1
				}
				else if(containi(read,"[1]")==0)
				{
					part = 1
					keys |= (1<<0)
				}
				else if(containi(read,"[2]")==0)
				{
					part = 2
					keys |= (1<<1)
				}
				else if(containi(read,"[3]")==0)
				{
					part = 3
					keys |= (1<<2)
				}
				else if(containi(read,"[4]")==0)
				{
					part = 4
					keys |= (1<<3)
				}
				else if(containi(read,"[5]")==0)
				{
					part = 5
					keys |= (1<<4)
				}
				else if(containi(read,"[6]")==0)
				{
					part = 6
					keys |= (1<<5)
				}
				else if(containi(read,"[7]")==0)
				{
					part = 7
					keys |= (1<<6)
				}
				else if(containi(read,"[8]")==0)
				{
					part = 8
					keys |= (1<<7)
				}
				else if(containi(read,"[9]")==0)
				{
					part = 9
					keys |= (1<<8)
				}
				else if(containi(read,"[0]")==0)
				{
					part = 10
					keys |= (1<<9)
				}
			}
			else
			{
				if(containi(read,"[/")==0)
				{
					if(containi(read,"[/Menu]")==0) part = 0
					else if(containi(read,"[/1]")==0) part = 0
					else if(containi(read,"[/2]")==0) part = 0
					else if(containi(read,"[/3]")==0) part = 0
					else if(containi(read,"[/4]")==0) part = 0
					else if(containi(read,"[/5]")==0) part = 0
					else if(containi(read,"[/6]")==0) part = 0
					else if(containi(read,"[/7]")==0) part = 0
					else if(containi(read,"[/8]")==0) part = 0
					else if(containi(read,"[/9]")==0) part = 0
					else if(containi(read,"[/0]")==0) part = 0
				}
				switch(part)
				{
					case -1: format(motd,1999,"%s^n%s",motd,read)
					case 1: format(execute[1],399,"%s;%s",execute[1],read)
					case 2: format(execute[2],399,"%s;%s",execute[2],read)
					case 3: format(execute[3],399,"%s;%s",execute[3],read)
					case 4: format(execute[4],399,"%s;%s",execute[4],read)
					case 5: format(execute[5],399,"%s;%s",execute[5],read)
					case 6: format(execute[6],399,"%s;%s",execute[6],read)
					case 7: format(execute[7],399,"%s;%s",execute[7],read)
					case 8: format(execute[8],399,"%s;%s",execute[8],read)
					case 9: format(execute[9],399,"%s;%s",execute[9],read)
					case 10: format(execute[0],399,"%s;%s",execute[0],read)
				}
			}
		}

		admin=id

		voting=true
		for(new i=0;i<10;i++) results[i]=0
		results[10]=-1

		show_menu(0,keys,motd,-1,"VoteMenu")
		set_task(25.0,"VoteTally")

		console_print(id,"[AMXX] Menu ^"%s^" started.",arg)
		client_print(0,print_chat,"[AMXX] Admin has initiated a vote.")
	}

	return PLUGIN_HANDLED
}

public VotePushed(id,key)
{
	if(key==9) results[0]++
	else results[key+1]++

	if(get_pcvar_num(show_pcvar))
	{
		new name[32]
		get_user_name(id,name,31)

		if(key==9) client_print(0,print_chat,"[AMXX] %s voted for # 0",name)
		else client_print(0,print_chat,"[AMXX] %s voted for # %d",name,key+1)
	}

	return PLUGIN_HANDLED
}

public VoteTally()
{
	new winner, winner2, bool:tie
	winner=10
	for(new i=0;i<10;i++)
	{
		if(results[i]>results[winner])
		{
			winner=i
			tie=false
		}
		else if(results[i]==results[winner])
		{
			winner2=i
			tie=true
		}
	}

	if(tie)
	{
		client_print(0,print_chat,"[AMXX] Voting Over: Tie between #%d and #%d. No action taken.",winner,winner2)
	}
	else
	{
		client_print(0,print_chat,"[AMXX] Voting Over: #%d won.",winner)
		if(get_pcvar_num(execute_pcvar)==1 && is_user_connected(admin)) client_cmd(admin,"%s",execute[winner])
		else server_cmd("%s",execute[winner])
	}

	voting=false
}
